import axios from "axios";
import qs from "query-string";

export default {
  /**
   * 其他项目通过token查询公文传输的未读数量
   * @method
   * @name getOtherAnnouncementCount
   */
  getOtherAnnouncementCount: (params = {}) => {
    let domain = "//" + window.sessionStorage.getItem("otherApi") + "/api";
    let path = "/announcement/other/count";
    let body = {};
    let queryParameters = params;
    let headers = {};
    let form = {};

    headers["Accept"] = "application/json";

    let url = domain + path;
    let options = {
      method: "GET",
      url: url,
      params: queryParameters,
      data: body,
      headers: headers
    };
    if (Object.keys(form).length > 0) {
      options.data = qs.stringify(form);
      options.headers["Content-Type"] =
        "application/x-www-form-urlencoded;charset=utf-8";
    }

    return axios(options).then(ret => {
      return ret;
    });
  },
  /**
   * 其他项目通过token查询公文传输收文列表（分页）
   * @method
   * @name getOtherAnnouncementPageList
   */
  getOtherAnnouncementPageList: (params = {}) => {
    let domain = "//" + window.sessionStorage.getItem("otherApi") + "/api";
    let path = "/announcement/other";
    let body = {};
    let queryParameters = {};
    let headers = {};
    let form = {};

    headers["Accept"] = "application/json";

    if (params["pageIndex"] !== undefined) {
      queryParameters["pageIndex"] = params["pageIndex"];
    }

    if (params["pageSize"] !== undefined) {
      queryParameters["pageSize"] = params["pageSize"];
    }

    if (params["keywords"] !== undefined) {
      queryParameters["keywords"] = params["keywords"];
    }

    if (params["categoryId"] !== undefined) {
      queryParameters["categoryId"] = params["categoryId"];
    }

    if (params["emergencyDegree"] !== undefined) {
      queryParameters["emergencyDegree"] = params["emergencyDegree"];
    }

    if (params["status"] !== undefined) {
      queryParameters["status"] = params["status"];
    }

    if (params["type"] !== undefined) {
      queryParameters["type"] = params["type"];
    }

    if (params["startTime"] !== undefined) {
      queryParameters["startTime"] = params["startTime"];
    }

    if (params["endTime"] !== undefined) {
      queryParameters["endTime"] = params["endTime"];
    }

    let url = domain + path;
    let options = {
      method: "GET",
      url: url,
      params: queryParameters,
      data: body,
      headers: headers
    };
    if (Object.keys(form).length > 0) {
      options.data = qs.stringify(form);
      options.headers["Content-Type"] =
        "application/x-www-form-urlencoded;charset=utf-8";
    }

    return axios(options).then(ret => {
      return ret;
    });
  },
  /**
   * 其他项目通过token和id查询公文传输,并且处理已读状态
   * @method
   * @name getOtherAnnouncementById
   */
  getOtherAnnouncementById: (params = {}) => {
    let domain = "//" + window.sessionStorage.getItem("otherApi") + "/api";
    let path = "/announcement/other/{id}";
    let body = {};
    let queryParameters = {};
    let headers = {};
    let form = {};

    headers["Accept"] = "application/json";

    path = path.replace("{id}", params["id"]);

    if (params["id"] === undefined) {
      return Promise.reject(new Error("Missing required  parameter: id"));
    }

    let url = domain + path;
    let options = {
      method: "GET",
      url: url,
      params: queryParameters,
      data: body,
      headers: headers
    };
    if (Object.keys(form).length > 0) {
      options.data = qs.stringify(form);
      options.headers["Content-Type"] =
        "application/x-www-form-urlencoded;charset=utf-8";
    }

    return axios(options).then(ret => {
      return ret;
    });
  },
  /**
   * 获取文件
   * @method
   * @name getFile
   */
  getFile: (params = {}) => {
    let domain = "//" + window.sessionStorage.getItem("otherApi") + "/api";
    let path = "/sys/file/{id}";
    let body = {};
    let queryParameters = {};
    let headers = {};
    let form = {};

    headers["Accept"] = "application/octet-stream";

    path = path.replace("{id}", params["id"]);

    if (params["id"] === undefined) {
      return Promise.reject(new Error("Missing required  parameter: id"));
    }

    let url = domain + path;
    let options = {
      method: "GET",
      url: url,
      params: queryParameters,
      data: body,
      headers: headers,
      responseType: "arraybuffer"
    };
    if (Object.keys(form).length > 0) {
      options.data = qs.stringify(form);
      options.headers["Content-Type"] =
        "application/x-www-form-urlencoded;charset=utf-8";
    }

    return axios(options).then(ret => {
      return ret;
    });
  },
  // 获取萤石token
  getToken: (params = {}) => {
    let path = '/yinshi-api';
    let body = {};
    let queryParameters = {};
    let headers = {};
    let form = {};

    headers["Accept"] = 'application/json'

    // if (params["appKey"] !== undefined) {
    //   body["appKey"] = params["appKey"]
    // }
    // if (params["appSecret"] !== undefined) {
    //   body["appSecret"] = params["appSecret"]
    // }
    path = path.replace("{appKey}", params["appKey"]);
    path = path.replace("{appSecret}", params["appSecret"]);
    let options = {
      method: 'POST',
      url: path,
      params: queryParameters,
      data: body,
      headers: headers
    }
    if (Object.keys(form).length > 0) {
      options.data = qs.stringify(form);
      options.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'
    }

    return axios(options).then(ret => {
      return ret;
    });
  }
};
